package com.nofx.mydemo.service.impl;

import com.google.common.collect.Maps;
import com.nofx.mydemo.service.IBizService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * @author yanyuchi
 * @date 2021-04-07 20:41
 */
@Service
public class BizService implements IBizService {

    private Map<String, Function<String,String>> dispatcher = Maps.newHashMap();


    @PostConstruct
    public void dispatcherInit(){
        dispatcher.put("key_订单1",order->String.format("对%s执行业务逻辑1",order));
        dispatcher.put("key_订单1_订单2", order->String.format("对%s执行业务逻辑2",order));
    }


    @Override
    public String check(String order, int level) {
        String key = getDispatchKey(order,level);

        Function<String, String> result = Optional.ofNullable(dispatcher.get(key)).orElse(k -> String.format("无效业务:%s", key));

        return result.apply(order);
    }

    private String getDispatchKey(String order, int level){
        StringBuilder key = new StringBuilder("key");
        for(int i = 1;i<=level;i++){
            key.append("_"+order+i);
        }
        return key.toString();
    }


}
